package cn.iocoder.yudao.module.agent.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.agent.base.MemberRespVO;
import cn.iocoder.yudao.module.agent.controller.admin.user.vo.AgentBindBrokerageReqVO;
import cn.iocoder.yudao.module.agent.controller.admin.user.vo.AgentBindSmsSendReqVO;
import cn.iocoder.yudao.module.agent.controller.admin.user.vo.AgentUserRespVO;
import cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount.vo.AgentWithdrawAccountPageReqVO;
import cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount.vo.AgentWithdrawAccountRespVO;
import cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount.vo.AgentWithdrawAccountUpdateReqVO;
import cn.iocoder.yudao.module.agent.controller.app.brokeragewithdrawrecord.vo.AppAgentBrokerageWithdrawRecordCreateReqVO;
import cn.iocoder.yudao.module.agent.convert.base.AgentConvert;
import cn.iocoder.yudao.module.agent.convert.user.AgentUserConvert;
import cn.iocoder.yudao.module.agent.convert.withdrawaccount.AgentWithdrawAccountConvert;
import cn.iocoder.yudao.module.agent.dal.dataobject.user.AgentUserDO;
import cn.iocoder.yudao.module.agent.dal.dataobject.withdrawaccount.AgentWithdrawAccountDO;
import cn.iocoder.yudao.module.agent.service.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordService;
import cn.iocoder.yudao.module.agent.service.user.AgentUserService;
import cn.iocoder.yudao.module.agent.service.withdrawaccount.AgentWithdrawAccountService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "APP - 代理人信息")
@RestController
@RequestMapping("/agent/user")
@Validated
public class AppAgentUserController {

    @Resource
    private AgentUserService userService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private AgentWithdrawAccountService withdrawAccountService;

    @Resource
    private AgentBrokerageWithdrawRecordService agentBrokerageWithdrawRecordService;

    @Resource
    private AgentWithdrawAccountService agentWithdrawAccountService;

    @GetMapping("/get")
    @Operation(summary = "获得代理人信息")
    public CommonResult<AgentUserRespVO> getByUserId() {
        AgentUserDO agentUserDO = userService.getByUserId(getLoginUserId());
        if(agentUserDO == null){
            return success(null);
        }

        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(agentUserDO.getUserId());
        MemberRespVO memberRespVO = AgentConvert.INSTANCE.covnertMember(memberUserRespDTO);

        AgentWithdrawAccountDO accountDO = withdrawAccountService.getWithdrawAccount(agentUserDO.getWithdrawId());
        AgentWithdrawAccountRespVO accountRespVO = AgentWithdrawAccountConvert.INSTANCE.convert(accountDO);

        return success(AgentUserConvert.INSTANCE.convert02(agentUserDO, memberRespVO, accountRespVO));
    }

    @GetMapping("/get-account-info")
    @Operation(summary = "获得代理人提现信息")
    public CommonResult<AgentWithdrawAccountRespVO> getWithdrawAccountInfo() {
        AgentWithdrawAccountDO accountDO = agentWithdrawAccountService.getWithdrawAccountByUserId(getLoginUserId());

        if(accountDO == null){
            return success(null);
        }

        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(accountDO.getUserId());
        MemberRespVO memberRespVO = AgentConvert.INSTANCE.covnertMember(memberUserRespDTO);

        return success(AgentWithdrawAccountConvert.INSTANCE.convert(accountDO, memberRespVO));
    }

    @PostMapping("/send-sms-code")
    @Operation(summary = "绑定分销员发送手机验证码")
    public CommonResult<Boolean> sendSmsCode(@RequestBody @Valid AgentBindSmsSendReqVO reqVO) {
        return success(userService.sendSmsCode(reqVO));
    }

    @PostMapping("bind-brokerage")
    @Operation(summary = "绑定分销人员")
    public CommonResult<Boolean> bindBrokerageUser(@RequestBody @Valid AgentBindBrokerageReqVO reqVO){
        return success(userService.bindBrokerageUser(reqVO));
    }

    @PostMapping("change-withdraw-account")
    @Operation(summary = "修改提现账户信息")
    public CommonResult<Boolean> changeWithdrawAccount(@RequestBody @Valid AgentWithdrawAccountUpdateReqVO updateReqVO){
        updateReqVO.setUserId(getLoginUserId());

        withdrawAccountService.updateWithdrawAccount(updateReqVO);
        return success(true);
    }

    @PostMapping("withdraw")
    @Operation(summary = "代理人请求提现")
    public CommonResult<Long> withdraw(@RequestBody @Valid AppAgentBrokerageWithdrawRecordCreateReqVO reqVO){
        return success(agentBrokerageWithdrawRecordService.appCreateBrokerageWithdrawRecord(reqVO));
    }
}
